We will run R using quarto documents in R studio
Download the latest versions:
Quarto: turns R into a text editor
#Make your interface look nice:
Fira code: https://github.com/tonsky/FiraCode/wiki/RStudio-instructionsFile > New project
Projects are powerful:
Open a quarto document in your new project
File > New File > Quarto document
Save the document within the project directory (where you already are)
Save the _quarto.yml provided in the email within this directory
Render the document
The base version of R can be upgraded with packages
We shall use the tidyverse collection of packages.
A collection of packages
All follow the same logic
Quite different from base R
“Supremely readable”
A Bavarian pterosaur from the Jurassic
140 different individuals have been found
Data from Habib and Hone 2024 PeerJ
# A tibble: 138 × 15
Individual_ID ORBIT SKULL NECK TRUNK_LENGTH TAIL HUMERUS RADIUS
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 1 11 40 21.5 47.5 106. 16.5 26.7
2 2 10 35 18 NA 112 15.3 26
3 3 NA NA NA 36 85 NA NA
4 4 NA 36 NA 39 115 17 28
5 5 NA NA 15.5 36.5 100 14.6 23.8
6 6 12 35 23 40 110 NA NA
7 7 10 35 20 41 106 15.5 24
8 8 NA 31 NA NA NA 14.5 24
9 9 NA NA 17 36 79 NA NA
10 10 12.5 41 23 47 125 19 32
# ℹ 128 more rows
# ℹ 7 more variables: METACARPAL_4 <dbl>, WING_PHALANX_1 <dbl>,
# WING_PHALANX_2 <dbl>, WING_PHALANX_3 <dbl>, WING_PHALANX_4 <dbl>,
# FEMUR <dbl>, TIBIA <dbl>
%>%This weird symbol is called a pipe
You should read this as then
do this, then do this…
allows you to chain your code
select(): order, rename or drop columns
filter(): keep or remove specific rows
mutate(): create new columns or edit existing ones
If you ever need help with a function, ? is your friend
Removing columns you aren’t interested in:
Choosing rows of interest
Large_data <- pterosaur_data %>% filter(TAIL > 200)
ten_cm_tails <- pterosaur_data %>% filter(TAIL == 100)
long_tails_and_small_heads <-
pterosaur_data %>% filter(TAIL > 200 & SKULL < 90)
long_tails_or_small_heads <-
pterosaur_data %>% filter(TAIL > 200 | SKULL < 90)| indicates or
TRUNK_LENGTH values, for individuals with IDs greater than 50Let’s change the units of measurement to centimetres
The total length of a wing is roughly the sum of the lengths of the humerus, radius, fourth metacarpal and the four wing phalanxs. With mutate(), we can calculate this and add it to the dataset:
Can we place individuals into phenotypic classes?
pterosaur_data_age_structured <-
pterosaur_data %>%
mutate(Phenotypic_class = case_when(
single_wing_length < 300 ~ "Small",
single_wing_length >= 300 ~ "Large",
.default = "Unknown"))
pterosaur_data_age_structured %>%
select(Individual_ID, Phenotypic_class, single_wing_length)# A tibble: 138 × 3
Individual_ID Phenotypic_class single_wing_length
<dbl> <chr> <dbl>
1 1 Small 183.
2 2 Small 174.
3 3 Unknown NA
4 4 Small 189.
5 5 Small 166.
6 6 Unknown NA
7 7 Small 164.
8 8 Unknown NA
9 9 Unknown NA
10 10 Small 221
# ℹ 128 more rows
It’s also possible to mutate a single row
Not every individual has a recorded wing length.
But there are other morphological traits in the dataset
Create a classification criteria and implement it
mutate() can be extended to summarise() row valuesgroup_by functionFocus on writing clear code, with comments (using the #) accompanying each important step.
Hint: the round() function can be used inside mutate()
Once complete, pass your polished dataframe to this function with the %>% to make a neat table
distinct()
slice()
n()
bind_rows()
What if we have two separate dataframes that we want to merge?
five_random_pterosaurs <- pterosaur_data %>%
filter_at(vars(2:15), all_vars(!is.na(.))) %>%
slice_sample(n = 8)
eye_stats <-
five_random_pterosaurs %>%
slice_sample(n = 5) %>%
select(Individual_ID, ORBIT) %>%
arrange(Individual_ID)
tail_stats <-
five_random_pterosaurs %>%
slice_sample(n = 5) %>%
select(Individual_ID, TAIL) %>%
arrange(Individual_ID)For joins to work, there needs to be some common element that links the two dataframes
y to dataframe xIndividual_IDxx that have a matching common element in yUse quarto to publish a report, documenting your code
Use this time to tidy your code
Use comments within code chunks
Write explanations outside of code chunks
See the _quarto.yml file